package cn.nai.DemoRan.demo02;

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class AlgoVisualizer {
    //TODO 自己的数据
    private Circle2[] circle2s;
    private AlgoFrame frame;  //视图
    private boolean isAnimated = true;

    public AlgoVisualizer(int sceneWidth,int sceneHeight,int N){
        circle2s = new Circle2[N];
        int R = 50;
        for (int i =0 ;i<N;i ++){
            int x = (int)(Math.random()*(sceneWidth-2*R))+R;
            int y = (int)(Math.random()*(sceneHeight-2*R))+R;
            int vx = (int)(Math.random()*11)-5;
            int vy = (int)(Math.random()*11)-5;
            circle2s[i] = new Circle2(x,y,R,vx,vy);

        }

        EventQueue.invokeLater(()->{
            frame = new AlgoFrame("welcome",sceneWidth,sceneHeight);
            //TODO 添加自己的监听器
            frame.addKeyListener(new AlgoKeyListener());
            frame.addMouseListener(new AlgoMouseListener());
            //            AlgoFrame frame = new AlgoFrame("welcome");
            //创建线程
            new Thread(()->{
                run();
            }).start();
        });
    }
    //动画逻辑
    private void run(){
        //TODO 自己的动画逻辑
        while (true) {
            //绘制数据
            frame.render(circle2s);
            AlgoVisHelper.pause(20);
            //更新数据
            if (isAnimated){
                for (Circle2 circle2 : circle2s) {
                    circle2.move(0,0,frame.getCanvasWidth(), frame.getCanvasHeight());
                }
            }
        }
    }

    public static void main(String[] args) {
        int sceneWidth = 800;
        int sceneHeight = 800;
        int N = 10;
        AlgoVisualizer visualizer = new AlgoVisualizer(sceneWidth,sceneHeight,N);
    }
    private class AlgoKeyListener extends KeyAdapter {
        @Override
        public void keyReleased(KeyEvent event){
             char c =event.getKeyChar();
            if (c == ' '){
                isAnimated = !isAnimated;
            }

        }


    }

    private class AlgoMouseListener extends MouseAdapter {
        @Override
        public void mousePressed(MouseEvent event){
            event.translatePoint(0,-(frame.getBounds().height-frame.getCanvasHeight()));
//            event.getPoint();
            for (Circle2 circle2 : circle2s)
                if (circle2.contain(event.getPoint()))
                    circle2.isFilled = !circle2.isFilled;
        }


    }

}

